/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unicen.twitter.suggest.frontend.controllers;

import ar.edu.unicen.twitter.suggest.backend.datasources.TwitterFacade;
import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioProxy;
import ar.edu.unicen.twitter.suggest.commons.dtos.Usuario;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.RejectedException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.TwitterException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.UserNotFoundException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.UserProtectedException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Alex
 */
@Controller("searchController")
public class SearchController {

    @Autowired
    TwitterFacade twitterFacade;
    
    private String screenName = "";
    private UsuarioProxy user;
    
    
    public SearchController() {
    }
    
    public void search(){
        
    }
    public String getScreenName() {
        return screenName;
    }

    public void setScreenName(String screenName) {
        try {
            this.screenName = screenName;        
            this.user = (UsuarioProxy) twitterFacade.getUser(screenName);
        } catch (UserNotFoundException ex) {
            Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UserProtectedException ex) {
            Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RejectedException ex) {
            Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TwitterException ex) {
            Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Usuario getResultUser(){
        return user;
    }
    public String getResultFollows(){
        if(user!=null){
            try {
                Collection<Usuario> follows = user.getFollows();
                JSONObject jsonResult = new JSONObject();           
                jsonResult.put("follows",follows);
                //return twitterFacade.getUserList(user.getFollows());
                return jsonResult.toString(); 
            } catch (JSONException ex) {
                Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
    public String getResultFollowers(){
        if(user!=null){
            try {
                Collection<Usuario> followers = (ArrayList<Usuario>) user.getFollowers();
                JSONObject jsonResult = new JSONObject();           
                jsonResult.put("followers",followers);
                return jsonResult.toString(); 
                //return twitterFacade.getUserList(user.getFollowers());
            }catch (JSONException ex) {
                Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
